﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Q7.BevTree.Test
{
    class Program
    {
        /// <summary>
        /// 任务配置
        /// </summary>
        public static List<TaskInfo> tasks = new List<TaskInfo>();
        /// <summary>
        /// 我的任务
        /// </summary>
        public static List<MyTask> myTasks = new List<MyTask>();

        static void Main(string[] args)
        {
            InitTaskTree();

            //接任务1
            AcceptTask(new BevTree.BevNodeInputParam { UserLvl = 1, UpId = "0", TaskId = "1" });

            //完成任务1
            //UpdateTask(new BevNodeInputParam { TaskId = "1", UserLvl = 22 });

            //接任务2 必须完成任务1
            AcceptTask(new BevTree.BevNodeInputParam { UserLvl = 2, UpId = "1", TaskId = "2" });
			Console.Read();
        }

        #region 初始化任务树
        /// <summary>
        /// 初始化任务树
        /// </summary>
        static void InitTaskTree()
        {
            //初始化任务1
            TaskInfo t1 = new TaskInfo() { UserLvl = 1, TaskId = "1", UpId = "0", TargetNum1 = 1, TargetNum2 = 0, ProgressNum1 = 1, ProgressNum2 = 0 };
            //任务条件
            t1.conditionRoot = (Condition.Create()
                .AddChild(Condition_LevelRequirement.Create())
                ) as Condition;
            //任务目标
            t1.objectiveRoot = (Objective.Create()
                .AddChild(Objective_LevelRequirement.Create())) as Objective;
            tasks.Add(t1);

            //初始化任务2
            TaskInfo t2 = new TaskInfo() { UserLvl = 2, TaskId = "2", UpId = "1", TargetNum1 = 1, TargetNum2 = 0, ProgressNum1 = 1, ProgressNum2 = 0 };
            //任务条件 等级和上一任务完成达到
            t2.conditionRoot = (Condition_AND.Create()
                .AddChild(Condition_LevelRequirement.Create())
                .AddChild(Condition_FinishTask.Create())) as Condition;
            //任务目标
            t2.objectiveRoot = (Objective.Create()
                .AddChild(Objective_LevelRequirement.Create())) as Objective;
            tasks.Add(t2);
        }
        #endregion

        #region 根据任务ID获取任务
        /// <summary>
        /// 根据任务ID获取配置文件
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public static TaskInfo GetTaskById(string taskId)
        {
            return tasks.Find((x) => x.TaskId == taskId);
        }

        /// <summary>
        /// 根据任务ID获取我的任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public static MyTask GetMyTaskById(string taskId)
        {
            return myTasks.Find((x) => x.TaskId == taskId);
        }
        #endregion

        #region 接任务
        /// <summary>
        /// 接任务
        /// </summary>
        /// <param name="taskId"></param>
        /// <param name="inputParam"></param>
        public static void AcceptTask(BevNodeInputParam inputParam)
        {
            TaskInfo q = GetTaskById(inputParam.TaskId);
            if (q.conditionRoot.IsTrue(inputParam))
            {
                //添加到我的任务列表
                myTasks.Add(new MyTask
                {
                    TaskId = inputParam.TaskId,
                    Status = 0,
                    TargetNum1 = q.TargetNum1,
                    TargetNum2 = q.TargetNum2,
                    ProgressNum1 = 0,
                    ProgressNum2 = 0
                });
                Console.WriteLine($"接任务{inputParam.TaskId}成功");
            }
            else
            {
                Console.WriteLine($"接任务{inputParam.TaskId}失败");
            }
        }
        #endregion

        #region 更新任务
        /// <summary>
        /// 更新任务
        /// </summary>
        /// <param name="inputParam"></param>
        public static void UpdateTask(BevNodeInputParam inputParam)
        {
            TaskInfo q = GetTaskById(inputParam.TaskId);
            bool isLvl = q.objectiveRoot.IsTrue(inputParam);
            if (isLvl)
            {
                EObjectiveState state = q.objectiveRoot.UpdateValue(inputParam);
                if (state == EObjectiveState.Updated)
                {
                    Console.WriteLine($"更新任务{inputParam.TaskId}");
                }
                else if (state == EObjectiveState.Finished)
                {
                    Console.WriteLine($"完成任务{inputParam.TaskId}");
                }
            }
            else
            {
                Console.WriteLine("任务没有变化");
            }
        }
        #endregion
    }
}
